package com.bit.frige;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;

public class GuideSlideActivity extends Activity implements OnTouchListener {
	/** ViewFlipper 컴포넌트 객체 */
	private ViewFlipper m_viewFlipper;
	/** ViewFilpper 안에서 터치된 X축의 좌표 */
	private int m_nPreTouchPosX = 0;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide_slide);
		m_viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
		m_viewFlipper.setOnTouchListener(this);

		// ViewFlipper에 서브 레이아웃 추가
		LinearLayout sub1 = (LinearLayout) View.inflate(this,
				R.layout.slide_sub1, null);
		LinearLayout sub2 = (LinearLayout) View.inflate(this,
				R.layout.slide_sub2, null);
		LinearLayout sub3 = (LinearLayout) View.inflate(this,
				R.layout.slide_sub3, null);
		m_viewFlipper.addView(sub1);
		m_viewFlipper.addView(sub2);
		m_viewFlipper.addView(sub3);
	}

	private void MoveNextView() {
		m_viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
				R.anim.appear_from_right));
		m_viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
				R.anim.disappear_from_left));
		m_viewFlipper.showNext();
	}

	private void MovewPreviousView() {
		m_viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
				R.anim.appear_from_left));
		m_viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
				R.anim.disappear_from_right));
		m_viewFlipper.showPrevious();
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			m_nPreTouchPosX = (int) event.getX();
		}

		if (event.getAction() == MotionEvent.ACTION_UP) {
			int nTouchPosX = (int) event.getX();

			if (nTouchPosX < m_nPreTouchPosX) {
				MoveNextView();
			} else if (nTouchPosX > m_nPreTouchPosX) {
				MovewPreviousView();
			}

			m_nPreTouchPosX = nTouchPosX;
		}

		return true;
	}

}

